/*********************************************************************
 * Software License Agreement (BSD License)
 *
 *  Copyright (c) 2024, USTC
 *  All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions
 *  are met:
 *
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above
 *     copyright notice, this list of conditions and the following
 *     disclaimer in the documentation and/or other materials provided
 *     with the distribution.
 *   * Neither the name of USTC nor the names of its
 *     contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 *  POSSIBILITY OF SUCH DAMAGE.
 *********************************************************************/

/* Author: Xinsheng Tang */
#pragma once
#include <geometry_msgs/Pose.h>
#include <jsoncpp/json/json.h>
#include <moveit/kinematic_constraints/utils.h>
#include <moveit/move_group_interface/move_group_interface.h>
#include <moveit/robot_model_loader/robot_model_loader.h>
#include <moveit_msgs/GetMotionPlan.h>
#include <moveit_msgs/GetMotionSequence.h>
#include <moveit_msgs/MotionPlanRequest.h>
#include <moveit_msgs/MotionSequenceRequest.h>
#include <ros/package.h>
#include <ros/ros.h>
#include <std_msgs/Int32.h>
#include <std_msgs/String.h>

#include <fstream>
#include <regex>
#include <string>
#include <unordered_map>

#define CLOSE_SPEED 0.1
#define CLOSE_ACC 0.05
#define NORMAL_SPEED 0.3
#define NORMAL_ACC 0.2
class manipulator {
 public:
  manipulator(ros::NodeHandle &nh,
              moveit::planning_interface::MoveGroupInterfacePtr mgptr);
  ~manipulator();
  /**
   * @brief 单段轨迹运动
   *
   * @param name 目标位姿或关节名称
   * @param speed 速度缩放系数
   * @param acc 加速度缩放系数
   * @param planner 规划方法(PTP 或 LIN)
   */
  void move_single(const std::string& name, double speed, double acc, const std::string& planner);
  /**
   * @brief 根据设定的操作序列进行移动(只支持包含ptp和lin运动)
   *
   * @param V_name 需要经过的路点名称
   * @param V_radius
   * 指定每个路点的融合半径系数(0,1)(最后一个路径点必须为0)。如下图所示,真实融合半径=融合半径系数*min(||Pm-1Pm||,||PmPm+1||)
   *  @image html blend.png "平滑过渡段示意图"
   * @param V_speed
   * 指定每个路点的速度缩放系数(0,1]。实际运动速度=速度缩放系数*最大运动速度
   * @param V_acc
   * 指定每个路点的加速度缩放系数(0,1]。实际运动加速度=加速度缩放系数*最大运动加速度
   * @param V_planner
   * 指定每个路点的规划器(PTP,LIN)。PTP为点到点规划，LIN为直线规划
   */
  void move_seq(const std::vector<std::string> &V_name, const std::vector<double> &V_radius,
                const std::vector<double> &V_speed, const std::vector<double> &V_acc,
                const std::vector<std::string> &V_planner);
  /**
   * @brief 从文件中将指定关节角载入到joints_中
   *
   * @param station_name
   */
  void load_joints(const std::string &station_name);
  moveit::planning_interface::MoveGroupInterfacePtr move_group_ptr_;

 private:
  std::unordered_map<std::string, std::vector<double>> joints_;
  const std::vector<std::string> joint_names_{
      "shoulder_pan_joint", "shoulder_lift_joint", "elbow_joint",
      "wrist_1_joint",      "wrist_2_joint",       "wrist_3_joint"};
  moveit::core::RobotModelPtr robotmodel_ptr_;
  ros::ServiceClient seq_client_;

  /**
   * @brief 从joints_中获取关节角信息
   *
   * @param name 关节角的名称
   * @return std::vector<double> 关节角数组
   */
  std::vector<double> get_joint(const std::string &name);
  /**
   * @brief 根据给定的操作，构造轨迹规划的request
   *
   * @param V_name 需要经过的路点名称
   * @param V_radius 每个路点的融合半径系数(0,1)(最后一个路径点必须为0)
   * @param V_speed 每个路点的速度系数(0,1]
   * @param V_acc 每个路点的加速度系数(0,1]
   * @param V_planner 每个路点的规划器(PTP,LIN)
   * @param validate 是否仅验证该操作是否可行
   * @return moveit_msgs::MotionSequenceRequest 构造得到的轨迹规划request
   */
  moveit_msgs::MotionSequenceRequest construct_seq_req(
      const std::vector<std::string> &V_name, const std::vector<double> &V_radius,
      const std::vector<double> &V_speed, const std::vector<double> &V_acc,
      const std::vector<std::string> &V_planner);
  /**
   * @brief 根据指定名称,计算下一个目标位置和约束
   *
   * @param name 指定路径点名称
   * @param pose_goal 需要构造的目标约束
   * @param next_pose 需要构造的目标位置
   */
  void get_constraint(const std::string& name, moveit_msgs::Constraints &pose_goal,
                      geometry_msgs::Pose &next_pose);
  /**
   * @brief 根据当前位置,目标位置,以及目标位置的下一位置计算融合半径
   *
   * @param curr_pose 当前位置
   * @param next_pose 目标位置
   * @param n_next_pose 目标位置的下一位置
   * @return double
   */
  double get_blend_radius(const geometry_msgs::Pose &curr_pose,
                          const geometry_msgs::Pose &next_pose,
                          const geometry_msgs::Pose &n_next_pose);

};

geometry_msgs::Pose toPose(const Eigen::Isometry3d &T);
